<?php 
namespace app\mall\model;
use think\Model;
use think\Db;
class User extends Model{
	protected $pk = 'uid';
	protected $table = 'ys_user';
    protected $field = true;        //自动忽略不存在的字段
	protected $validate = [
		['realname', 'require', '请填写会员姓名'],
        ['mobile', 'require|unique:user', '手机号必填|该手机号已被占用，请重新填写'],
        // ['logo', 'require', '商品logo不能为空'],
//        ['email', 'unique:user', '该邮箱已被占用，请重新填写'],
	];
	protected function initialize(){
        parent::initialize();
    }
    /**
     * 新增 -- 编辑
     * @Author   lingyun
     * @DateTime 2017-11-29T10:42:35+0800
     * @param    [type]                   $data [description]
     */
    public function add_edit($data){
        try{
            if(isset($data['uid']) && $data['uid']>0){        //更新
                $result = $this->validate($this->validate)->save($data,['uid',$data['uid']]);
                if(false === $result){
                    return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
                }else{
                    return ['code' => 1, 'data' => '', 'msg' => '编辑成功','uid'=>$data['uid']];
                }
            }else{      //新增
                $result = $this->validate($this->validate)->save($data);
                if(!$result){
                    return ['code'=>0,'msg'=>$this->getError()];
                }else{
                    return ['code'=>1,'msg'=>'添加成功！','uid'=>$this->uid];
                }
            }
        }catch( PDOException $e){
            return ['code' => 0, 'data' => '', 'msg' => $e->getMessage()];
        }
    }
    /**
     * 修改密码
     * @Author   lingyun
     * @DateTime 2017-11-29T10:59:22+0800
     * @return   [type]                   [description]
     */
    public function pass($data){
        if(empty($data['password']))
            return ['code'=>0,'msg'=>'请输入密码'];
        if(!preg_match("/[a-zA-Z0-9_]{6,16}/i",$data['password']))
            return ['code'=>0,'msg'=>'密码是字母数字下划线6~16位组合'];
        if($data['password'] != $data['repass'])
            return ['code'=>'0','msg'=>'两次密码填写不一致'];
        $data['password'] = md5($data['password']);
        $res = $this->validate(false)->save($data,['uid',$data['uid']]);
        if(false === $res){
            return ['code' => 0, 'data' => '', 'msg' => $this->getError()];
        }else{
            return ['code' => 1, 'data' => '', 'msg' => '编辑成功','uid'=>$data['uid']];
        }
    }

}